﻿#pragma strict

var floor : GameObject;
var rooms : GameObject[];
var floors : GameObject[];
var maxFloors : int;
var maxHouseWidth : int;

private var currentFloor : int;
private var floorID : int;
private var houseWidth : int;
private var roomID : int; 
private var state : String;



function Start () {
	currentFloor = 0;
	floors = new GameObject[maxFloors+1];
	state = "floorBuilding";
}

function Update () {

if(state == "floorBuilding"){
	var floorInstance : GameObject;
	
	if(currentFloor <= maxFloors){
		floorInstance = Instantiate(floor, transform.position, transform.rotation);
		floorInstance.name = "floor"+currentFloor;
		floorInstance.transform.position.y = currentFloor;
		floors[currentFloor] = GameObject.Find("floor"+currentFloor);
		currentFloor = currentFloor + 1;
		} 
	if(currentFloor == floors.length){
		setRoomBuilder();
		Debug.Log("builderReady");
		}

	}


if(floorID < floors.Length && state == "roomBuilderReady"){

	if(houseWidth < maxHouseWidth){
		var room : GameObject = rooms[Random.Range(0, rooms.length)];
		var nextRoom : float = houseWidth + room.transform.localScale.x / 2;
		var currentHouseWidth : int = houseWidth + room.transform.localScale.x;
		
		if(currentHouseWidth <= maxHouseWidth){
			var roomInstance = Instantiate(room, Vector3(nextRoom,floors[floorID].transform.position.y,floors[floorID].transform.position.z), transform.rotation);
			houseWidth = houseWidth + roomInstance.transform.localScale.x;
			roomInstance.transform.parent = floors[floorID].transform;
			roomInstance.name = "room"+roomID;
			floors[floorID].SendMessage("roomPosition", roomID);
			roomID++;
			}
		}
	
	if(houseWidth == maxHouseWidth){
		floors[floorID].SendMessage("floorComplete", floorID);
		floorID++;
		roomID = 1;
		houseWidth = Mathf.RoundToInt(floors[floorID].transform.position.x);	
		}
}

}

function setRoomBuilder(){
	houseWidth = Mathf.RoundToInt(floors[floorID].transform.position.x);
	roomID = 0;
	state = "roomBuilderReady";
}




	
	
	



